package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.EventGroup;
import com.boatAnchorAssistant.pojo.param.eventGroup.*;
import com.boatAnchorAssistant.pojo.vo.eventGroup.*;
import com.boatAnchorAssistant.pojo.dto.eventGroup.*;
import com.boatAnchorAssistant.service.check.ClassUtil;
import com.boatAnchorAssistant.service.repository.in.EventGroupRepository;
import com.boatAnchorAssistant.service.service.in.EventGroupService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：事件分组服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class EventGroupServiceImpl implements EventGroupService {

    private final EventGroupRepository eventGroupRepository;
    private final ClassUtil<EventGroup> classUtil;

    public EventGroupServiceImpl(EventGroupRepository eventGroupRepository, ClassUtil<EventGroup> classUtil) {
        this.eventGroupRepository = eventGroupRepository;
        this.classUtil = classUtil;
    }

    void check(Boolean isSave,EventGroup eventGroup){
       try {
           if (isSave) {
               classUtil.addIsHave(eventGroup, eventGroupRepository);
           } else {
               classUtil.updateIsHave(eventGroup, eventGroupRepository,eventGroup.getId());
           }
       }
       catch (Exception e){
           e.printStackTrace();
           throw new BizException(e.getMessage());
       }
    }

    /**
     * 创建事件分组
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createEventGroup(CreateEventGroupParam param) {
        EventGroup eventGroup = new EventGroup();
        BeanUtils.copyProperties(param, eventGroup);
        check(true,eventGroup);
        Long id = IdGenerator.SNOW.generate();
        eventGroup.setId(id);
        eventGroupRepository.create(eventGroup);
    }

    /**
     * 修改事件分组
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEventGroup(EditEventGroupParam param) {
        Long id = param.getId();
        EventGroup eventGroup = eventGroupRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, eventGroup);
        check(false,eventGroup);
        eventGroupRepository.edit(eventGroup);
    }

    /**
     * 移除事件分组
     *
     * @param id
     */
    @Override
    public void removeEventGroup(Long id) {
        eventGroupRepository.removeById(id);
    }

    /**
     * 启动/禁用事件分组
     *
     * @param id
     */
    @Override
    public void enabledEventGroup(Long id) {
        EventGroup eventGroup = eventGroupRepository.getByIdCheck(id);
        eventGroup.setIsDisable(!eventGroup.getIsDisable());
        eventGroupRepository.edit(eventGroup);
    }

    /**
     * 获取事件分组详情
     *
     * @param id
     * @return
     */
    @Override
    public GetEventGroupVo getEventGroup(Long id) {
        EventGroup eventGroup = eventGroupRepository.getByIdCheck(id);
        GetEventGroupVo vo = new GetEventGroupVo();
        BeanUtils.copyProperties(eventGroup, vo);
        return vo;
    }

    /**
     * 获取事件分组 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetEventGroupListVo> getEventGroupList(GetEventGroupListParam param) {

        SelectEventGroupDto dto = new SelectEventGroupDto();
        BeanUtils.copyProperties(param, dto);
        Page<EventGroup> page = eventGroupRepository.selectEventGroup(dto);
        List<EventGroup> eventGroupList = page.getRecords();
        List<GetEventGroupListVo> list = new ArrayList<>();
        for (EventGroup eventGroup : eventGroupList) {
            GetEventGroupListVo vo = new GetEventGroupListVo();
            BeanUtils.copyProperties(eventGroup, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取事件分组下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetEventGroupDownListVo> getEventGroupDownList(GetEventGroupDownListParam param) {
        EventGroup po = new EventGroup();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<EventGroup> eventGroupList = eventGroupRepository.getListByEntity(po);

        List<GetEventGroupDownListVo> list = new ArrayList<>();
        for (EventGroup eventGroup : eventGroupList) {
            GetEventGroupDownListVo vo = new GetEventGroupDownListVo();
            BeanUtils.copyProperties(eventGroup, vo);
            list.add(vo);
        }
        return list;
    }


}



